package com.moleq.device;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DatabaseHelper;
import com.moleq.models.Product;

public class DeviceSettingService {

	
	private DatabaseHelper DBHelper;
	
	public DeviceSettingService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
	}

	public ScannerSetting getScannerSetting(int num){
		ScannerSetting obj = new ScannerSetting();
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery(
				"SELECT *  FROM t_scanner_setting  Where  sn_using ='Y' and sn_port =" +String.valueOf(num),
				null);
		if (cursor.moveToFirst()) {
			// long lid = cursor.getLong(cursor.getColumnIndex("P_ID"));
			int iNo 		   = cursor.getInt(cursor.getColumnIndex("sn_no"));
			int iPort          = cursor.getInt(cursor.getColumnIndex("sn_port"));
			String sDeviceName = cursor.getString(cursor.getColumnIndex("sn_device_name"));
			String sBaud       = cursor.getString(cursor.getColumnIndex("sn_baud"));
			String sParity     = cursor.getString(cursor.getColumnIndex("sn_parity"));
			int iDataBits      = cursor.getInt(cursor.getColumnIndex("sn_data_bits"));
			int iStopBits      = cursor.getInt(cursor.getColumnIndex("sn_stop_bits"));
			String sDisableCmd = cursor.getString(cursor.getColumnIndex("sn_disable_cmd"));
			String sEnableCmd  = cursor.getString(cursor.getColumnIndex("sn_enable_cmd"));
			String sEndChar    = cursor.getString(cursor.getColumnIndex("sn_end_char"));
			String sChkDigit   = cursor.getString(cursor.getColumnIndex("sn_chk_digit"));
			String sDataFormat = cursor.getString(cursor.getColumnIndex("sn_data_format"));
			int iWeightFormat  = cursor.getInt(cursor.getColumnIndex("sn_weight_format"));
			String sUsing      = cursor.getString(cursor.getColumnIndex("sn_using"));
			String sCreateDatetime = cursor.getString(cursor.getColumnIndex("create_datetime"));
			String sUpdateDatetime = cursor.getString(cursor.getColumnIndex("update_datetime"));
			
			
			obj.setNO(iNo);
			obj.setPort(iPort);
			obj.setDeviceName(sDeviceName);
			obj.setBaud(sBaud);
			obj.setParity(sParity);
			obj.setDataBits(iDataBits);
			obj.setStopBits(iStopBits);
			obj.setDisableCmd(sDisableCmd);
			obj.setEnableCmd(sEnableCmd);
			obj.setEndChar(sEndChar);
			obj.setChkDigit(sChkDigit);
			obj.setDataFormat(sDataFormat);
			obj.setWeightFormat(iWeightFormat);
			obj.setUsing(sUsing);
//			obj.setCreateDateitme(sCreateDatetime);
//			obj.setUpdateDatetime(sUpdateDatetime);
			
		}

		cursor.close();
		return obj;
	}
	
	public List< ScannerSetting> getScannerSettings() {
		
		 List< ScannerSetting> settings = new ArrayList<ScannerSetting>();
		ScannerSetting obj = new ScannerSetting();
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery(
				"SELECT *  FROM t_scanner_setting  Where  sn_using ='Y'" ,
				null);
		while (cursor.moveToNext()) {
			// long lid = cursor.getLong(cursor.getColumnIndex("P_ID"));
			int iNo 		   = cursor.getInt(cursor.getColumnIndex("sn_no"));
			int iPort          = cursor.getInt(cursor.getColumnIndex("sn_port"));
			String sDeviceName = cursor.getString(cursor.getColumnIndex("sn_device_name"));
			String sBaud       = cursor.getString(cursor.getColumnIndex("sn_baud"));
			String sParity     = cursor.getString(cursor.getColumnIndex("sn_parity"));
			int iDataBits      = cursor.getInt(cursor.getColumnIndex("sn_data_bits"));
			int iStopBits      = cursor.getInt(cursor.getColumnIndex("sn_stop_bits"));
			String sDisableCmd = cursor.getString(cursor.getColumnIndex("sn_disable_cmd"));
			String sEnableCmd  = cursor.getString(cursor.getColumnIndex("sn_enable_cmd"));
			String sEndChar    = cursor.getString(cursor.getColumnIndex("sn_end_char"));
			String sChkDigit   = cursor.getString(cursor.getColumnIndex("sn_chk_digit"));
			String sDataFormat = cursor.getString(cursor.getColumnIndex("sn_data_format"));
			int iWeightFormat  = cursor.getInt(cursor.getColumnIndex("sn_weight_format"));
			String sUsing      = cursor.getString(cursor.getColumnIndex("sn_using"));
			String sCreateDatetime = cursor.getString(cursor.getColumnIndex("create_datetime"));
			String sUpdateDatetime = cursor.getString(cursor.getColumnIndex("update_datetime"));
			
			obj = new ScannerSetting();
			obj.setNO(iNo);
			obj.setPort(iPort);
			obj.setDeviceName(sDeviceName);
			obj.setBaud(sBaud);
			obj.setParity(sParity);
			obj.setDataBits(iDataBits);
			obj.setStopBits(iStopBits);
			obj.setDisableCmd(sDisableCmd);
			obj.setEnableCmd(sEnableCmd);
			obj.setEndChar(sEndChar);
			obj.setChkDigit(sChkDigit);
			obj.setDataFormat(sDataFormat);
			obj.setWeightFormat(iWeightFormat);
			obj.setUsing(sUsing);
//			obj.setCreateDateitme(sCreateDatetime);
//			obj.setUpdateDatetime(sUpdateDatetime);
			
			settings.add(obj);
		}

		cursor.close();
		return settings;
	}

	
	
	public ScaleSetting getScaleSetting(int num) {
		// TODO Auto-generated method stub
		ScaleSetting obj = new ScaleSetting();
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery(
				"SELECT *  FROM t_scale_setting  Where sl_using ='Y' and sl_port =" + String.valueOf(num),
				null);
		if(cursor.moveToFirst()){
			int iNo 		   = cursor.getInt(cursor.getColumnIndex("sl_no"));
			int iPort          = cursor.getInt(cursor.getColumnIndex("sl_port"));
			String sDeviceName = cursor.getString(cursor.getColumnIndex("sl_device_name"));
			String sBaud       = cursor.getString(cursor.getColumnIndex("sl_baud"));
			String sParity     = cursor.getString(cursor.getColumnIndex("sl_parity"));
			int iDataBits      = cursor.getInt(cursor.getColumnIndex("sl_data_bits"));
			int iStopBits      = cursor.getInt(cursor.getColumnIndex("sl_stop_bits"));
			String sWeightCmd = cursor.getString(cursor.getColumnIndex("sl_weight_cmd"));
			String sCancelCmd  = cursor.getString(cursor.getColumnIndex("sl_cancel_cmd"));
			String sEndChar    = cursor.getString(cursor.getColumnIndex("sl_end_char"));
			int iTimeOut   = cursor.getInt(cursor.getColumnIndex("sl_timeout"));
			String sDataFormat = cursor.getString(cursor.getColumnIndex("sl_data_format"));
			int iDivisor  = cursor.getInt(cursor.getColumnIndex("sl_divisor"));
			String sUsing      = cursor.getString(cursor.getColumnIndex("sl_using"));
			String sCreateDatetime = cursor.getString(cursor.getColumnIndex("create_datetime"));
			String sUpdateDatetime = cursor.getString(cursor.getColumnIndex("update_datetime"));
			
			obj.setNO(iNo);
			obj.setPort(iPort);
			obj.setDeviceName(sDeviceName);
			obj.setBaud(sBaud);
			obj.setParity(sParity);
			obj.setDataBits(iDataBits);
			obj.setStopBits(iStopBits);
			obj.setWeightCmd(sWeightCmd);
			obj.setCancelCmd(sCancelCmd);
			obj.setEndChar(sEndChar);
			obj.setTimeOut(iTimeOut);
			obj.setDataFormat(sDataFormat);
			obj.setDivisor(iDivisor);
			obj.setUsing(sUsing);
//			obj.setCreateDateitme(sCreateDatetime);
//			obj.setUpdateDatetime(sUpdateDatetime);
		}
		
		cursor.close();
		return obj;
	}
	
	public List< ScaleSetting> getScaleSettings() {
		// TODO Auto-generated method stub
		 List< ScaleSetting> settings = new ArrayList<ScaleSetting>();
		ScaleSetting obj = new ScaleSetting();
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery(
				"SELECT *  FROM t_scale_setting  Where sl_using ='Y' ",
				null);
		while(cursor.moveToNext()){
			int iNo 		   = cursor.getInt(cursor.getColumnIndex("sl_no"));
			int iPort          = cursor.getInt(cursor.getColumnIndex("sl_port"));
			String sDeviceName = cursor.getString(cursor.getColumnIndex("sl_device_name"));
			String sBaud       = cursor.getString(cursor.getColumnIndex("sl_baud"));
			String sParity     = cursor.getString(cursor.getColumnIndex("sl_parity"));
			int iDataBits      = cursor.getInt(cursor.getColumnIndex("sl_data_bits"));
			int iStopBits      = cursor.getInt(cursor.getColumnIndex("sl_stop_bits"));
			String sWeightCmd = cursor.getString(cursor.getColumnIndex("sl_weight_cmd"));
			String sCancelCmd  = cursor.getString(cursor.getColumnIndex("sl_cancel_cmd"));
			String sEndChar    = cursor.getString(cursor.getColumnIndex("sl_end_char"));
			int iTimeOut   = cursor.getInt(cursor.getColumnIndex("sl_timeout"));
			String sDataFormat = cursor.getString(cursor.getColumnIndex("sl_data_format"));
			int iDivisor  = cursor.getInt(cursor.getColumnIndex("sl_divisor"));
			String sUsing      = cursor.getString(cursor.getColumnIndex("sl_using"));
			String sCreateDatetime = cursor.getString(cursor.getColumnIndex("create_datetime"));
			String sUpdateDatetime = cursor.getString(cursor.getColumnIndex("update_datetime"));
			
			obj = new ScaleSetting();
			obj.setNO(iNo);
			obj.setPort(iPort);
			obj.setDeviceName(sDeviceName);
			obj.setBaud(sBaud);
			obj.setParity(sParity);
			obj.setDataBits(iDataBits);
			obj.setStopBits(iStopBits);
			obj.setWeightCmd(sWeightCmd);
			obj.setCancelCmd(sCancelCmd);
			obj.setEndChar(sEndChar);
			obj.setTimeOut(iTimeOut);
			obj.setDataFormat(sDataFormat);
			obj.setDivisor(iDivisor);
			obj.setUsing(sUsing);
//			obj.setCreateDateitme(sCreateDatetime);
//			obj.setUpdateDatetime(sUpdateDatetime);
			
			settings.add(obj);
		}
		
		cursor.close();
		return settings;
	}

}
